<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class TruthDareController extends BaseController
{
    /**
     * @api {post} /truth-dare 新增真心话大冒险
     * @apiGroup 真心话大冒险
     * @apiParam {String} title_name 标题，必须
     * @apiParam {Int} title_type 类型：1：真心话；2：大冒险，必须
     * @apiParam {Int} status 状态：1：显示；0：隐藏，必须
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('title_name', 'title_type', 'status');
        $v = app('validator')->make($param, [
            'title_name' => 'required|string|max:50',
            'title_type' => 'required|integer|in:1,2',
            'status'     => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        $res = app('repo_truth_dare')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /truth-dare 编辑真心话大冒险
     * @apiGroup 真心话大冒险
     * @apiParam {Int} id 要编辑的记录id，必须
     * @apiParam {String} title_name 标题，必须
     * @apiParam {Int} title_type 类型：1：真心话；2：大冒险，必须
     * @apiParam {Int} status 状态：1：显示；0：隐藏，必须
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'title_name', 'title_type', 'status');
        $v = app('validator')->make($param, [
            'id'         => 'required|integer',
            'title_name' => 'required|string|max:50',
            'title_type' => 'required|integer|in:1,2',
            'status'     => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        $res = app('repo_truth_dare')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {delete} /truth-dare 删除真心话大冒险
     * @apiGroup 真心话大冒险
     * @apiParam {Int} id 要编辑的记录id，必须
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $id = $request->id;

        $truthDare = app('model_truth_dare')->where('id', $id)->first();
        if (empty($truthDare)) {
            throw new BasicException(0, 'id错误，记录不存在');
        }

        $res = $truthDare->delete();
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '删除失败');
    }

    /**
     * @api {get} /truth-dare 真心话大冒险列表
     * @apiGroup 真心话大冒险
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [
     * {
     * "id": 1, 序号
     * "title": "你的初吻是在几岁在什么地方被什么人夺去的？", 标题
     * "type": 1, 类型：1：真心话；2：大冒险
     * "status": 1, 状态：1：显示；0：隐藏
     * "created_at": "2021-08-11 14:20:49" 创建时间
     * },
     * {
     * "id": 3,
     * "title": "发微信朋友圈：亲人们，我当爸爸/妈妈了（24小时候才能删除）",
     * "type": 2,
     * "status": 0,
     * "created_at": "2021-08-11 14:21:56"
     * }
     * ],
     * "first_page_url": "http://local.qipaoadmin/truth-dare?page=1",
     * "from": 1,
     * "last_page": 1,
     * "last_page_url": "http://local.qipaoadmin/truth-dare?page=1",
     * "next_page_url": null,
     * "path": "http://local.qipaoadmin/truth-dare",
     * "per_page": 10,
     * "prev_page_url": null,
     * "to": 2,
     * "total": 2
     * }
     * }
     */
    public function list(Request $request)
    {
        $param = $request->only('page', 'page_size');
        $v = app('validator')->make($param, [
            'page'      => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        $pageSize = $param['page_size'] ?? 10;

        $list = app('model_truth_dare')
            ->select('id', 'title', 'type', 'status', 'created_at')
            ->paginate($pageSize);

        return Response::sendData($list);
    }
}